*****************************************************
*****************datamanagement dofile***************
*****************************************************

clear all
clear matrix
set more off, perm
cd "D:/Data/ProjData/Organizations, political participation/mod/"
do "C:/Userdata/Shared/Dofiles/DoData/Organizations, political participation/programs.do"

capture log close
log using "C:/Userdata/Shared/Logs/Organizations, political participation/datamanagement_turnout.text", replace text 


* Ulf data *
	
use "ulf_2000.dta", clear
append using "ulf_2001"
gen period = 1
append using "ulf_2008"
append using "ulf_2009"
replace period = 2 if ulfyear > "2001"

duplicates tag LopNr, gen(d)
keep if d==1																	/*Keeping those that are in the panel*/
drop d

* Adding voter turnout data 2009 * 													

merge m:1 LopNr using "Valdelt_2009.dta" 										
keep if _merge == 3	
drop _merge	

* Adding Lisa data for covariates

merge 1:1 LopNr ulfyear using "LISA_panel"
drop if _merge == 2
drop _merge

* Adding place of residence and inhabitants per km2

merge 1:1 LopNr ulfyear using "kommunkod_panel.dta", nogen keep(1 3)
merge m:1 Kommunkod ulfyear using "invkm2.dta", nogen keep(1 3)

* Adding data from Födelseregistret for addional covariates

merge m:1 LopNr using "Fodelseuppg.dta"
drop if _merge == 2
drop _merge

gen birthyear = real(substr(string(FodArMan),1,4))
destring ulfyear, replace
gen age = ulfyear - birthyear

gen gender = .
replace gender = 0 if Kon == 1
replace gender = 1 if Kon == 2

gen immig = 0
replace immig = 1 if F_delselandGrupp != "Sverige"

* Defining variables for voter turnout.

gen turnout_eu = 0
replace turnout_eu = 1 if e == "2" | e == "4" | e == "5" | e == "6"
replace turnout_eu = . if e == "3"
replace turnout_eu = 1 if s_turnout_eu == 1										/*Survey data on turnout for period 1*/
replace turnout_eu = . if s_turnout_eu == . & ulfyear < 2008

*Creating addtional variables for member of organizations*

destring memberboende memberculture memberpension membersport memberpeace memberculture memberpension membersport memberpeace, replace force
egen membernumberorg = rowtotal(memberboende memberculture memberpension membersport memberpeace)
egen activenumberorg = rowtotal(activeboende activeculture activepension activesport activepeace)

foreach k in member active {
	gen `k'any = 0
	replace `k'any = 1 if `k'boende == 1 | `k'culture == 1 | `k'pension == 1 | `k'sport == 1 | `k'peace == 1  
	replace `k'any = . if `k'boende == . | `k'culture == . & `k'pension == . & `k'sport == . & `k'peace == .
}

* Drop of variables and destring*

keep LopNr ulfyear period member* active* LoneInk yearseducation socialbidragfamilj sysselsatt age gender immig turnout_eu invkm2 
destring sysselsatt socialbidragfamilj yearseducation invkm2, replace

*Standardize income for each year in the survey

destring LoneInk, replace
egen mean_LoneInk = mean(LoneInk), by(ulfyear)
egen sd_LoneInk = sd(LoneInk), by(ulfyear)
gen zLoneInk = (LoneInk - mean_LoneInk) / sd_LoneInk
drop mean_LoneInk sd_LoneInk

*Recoding variables for active in organizations*

foreach k in any numberorg sport pension culture peace boende {
	replace active`k' = 0 if active`k' == . & member`k' != .
}

* Order and labels*
order LopNr ulfyear period turnout_eu

rename membernumberorg Nmemberships
rename activenumberorg Nactive

label var memberany "Member dummy"
label var activeany "Active member dummy"
label var Nmembership "Number memberships"
label var Nactive "Number active memberships"
label var memberboende "Member Housing"
label var activeboende "Active Housing"
label var membersport "Member Sport"
label var activesport "Active Sport"
label var memberpension "Member Pension"
label var activepension "Active Pension"
label var memberculture "Member Culture"
label var activeculture "Active Culture"
label var memberpeace "Member Peace"
label var activepeace "Active Peace"

****************************
save "dataanalysis_turnout.dta", replace
clear all
capture log close
